<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>文章列表--layui后台管理模板 2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="resources/css/public.css" media="all" />
</head>
<body class="childrenBody">
<form class="layui-form" style="width:80%;" lay-filter="dataFrm" id="dataFrm">
    <div class="layui-form-item layui-row layui-col-xs12">
        <label class="layui-form-label">手机号:</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input telephone" lay-verify="required" name="telephone" placeholder="请输入手机号">
        </div>
    </div>
    <div class="layui-form-item layui-row layui-col-xs12">
        <label class="layui-form-label">密码</label>
        <div class="layui-input-block">
            <input type="password" class="layui-input password" lay-verify="required" name="password" placeholder="请输入密码">
        </div>
    </div>
    <div class="layui-form-item layui-row layui-col-xs12">
        <label class="layui-form-label">姓名:</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input realname" lay-verify="required" name="realname" placeholder="请输入用户名">
        </div>
    </div>
    <div class="layui-form-item layui-row layui-col-xs12">
        <label class="layui-form-label">生日:</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input birthday" lay-verify="required" name="birthday" placeholder="请输入生日" id="birthday">
        </div>
    </div>
    <div class="layui-form-item layui-row layui-col-xs12">
        <label class="layui-form-label">上传图像:</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="test1">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
            <input type="hidden" id="himg" name="headimg">
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="demo1" src="resources/images/nopic.jpg" width="200px" height="200px">
                <p id="demoText"></p>
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-row layui-col-xs12">
        <label class="layui-form-label">所属部门:</label>
        <div class="layui-input-block">
            <select id="userDept" name="dept.id" class="userGrade" lay-filter="userDept">

            </select>
        </div>
    </div>
    <div class="layui-form-item layui-row layui-col-xs12">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <select name="available" class="available" lay-filter="userStatus">
                <option>------------请选择------------</option>
                <option value="1">正常使用</option>
                <option value="2">限制用户</option>
            </select>
        </div>
    </div>
    </div>
    <div class="layui-form-item layui-row layui-col-xs12">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="addUser">立即添加</button>
            <button type="reset" class="layui-btn layui-btn-sm layui-btn-primary">取消</button>
        </div>
    </div>
</form>
<script type="text/javascript" src="resources/layui/layui.js"></script>
<script th:inline="none">
    var html = "";
    layui.use(['form','layer','table','laytpl','laydate','upload'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            laytpl = layui.laytpl,
            table = layui.table,
            laydate=layui.laydate,
            upload = layui.upload;
            //绑定日期插件
            laydate.render({
                elem: '#birthday'
            });
            //文件上传
            upload.render({
                elem: '#test1',
                url: '/upload',
                field: 'headImage',
                before:function (res) {
                    //上传之前预读本地文件示例
                    res.preview(function (index,file,result) {
                        $("#demo1").attr("src",result);
                    })
                },
                done:function (res) {
                    //上传完毕回调
                    console.log(res.data)
                    $("#himg").val(res.data);
                    layer.msg("上传成功！");
                },
                error:function (res) {
                    //请求异常回调
                    console.log(res)
                }
            });
            //动态绑定下拉菜单的数据
            $.ajax({
                url:'depts',
                success:function (data) {
                    html += "<option value=''>------------请选择------------</option>"
                    for (i = 0; i < data.length; i++) {
                        html += ("<option value='" + data[i].id + "'>" + data[i].dname + "</option>")
                    }
                    $("#userDept").append(html);
                    form.render('select');
                }
            });
            //增加用户
            form.on("submit(addUser)",function (data) {
               // var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
                //实际使用时的提交数据
                var params=$("#dataFrm").serialize();
                console.log(params);
                $.post("user/insertUser.do",params,function (res) {
                    console.log(res)
                    top.layer.close(index);
                    top.layer.msg("用户添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                },"json");
            });
    })
</script>

</body>
</html>